/**
 * 检测渠道字段信息是否不全
 */
import React from 'react';
import { message, Modal, Table } from 'antd';
import { history } from 'umi'
import helper from '@/utils/auth/helper';

const { checkCode } = helper;

export default function useChannelError() {
    const checkError = (res, itemData = {}) => {
        const { data, code, msg = '' } = res;
        const { checkType = '1', checkData = [] } = data || {};
        return new Promise(resolve => {
            if (code === 10000) {
                if (String(checkType) === '1') {
                    resolve(false);
                }

                if (String(checkType) === '3') {
                    const columns = [
                        {
                            title: '渠道',
                            dataIndex: 'channelName',
                            width: 200
                        },
                        {
                            title: '字段',
                            dataIndex: 'columnNameList'
                        }
                    ];

                    const dataSource = checkData.map(({ channelCode, channelName, columnNameList = [] }) => ({
                        key: channelCode,
                        channelName,
                        columnNameList: columnNameList.join('、')
                    }));

                    Modal.confirm({
                        title: '提示',
                        content: (
                            <div>
                                <div style={{ marginBottom: 16 }}>
                                    以下渠道字段对应信息不全，请先进入字段配置页进行配置
                                </div>
                                <Table columns={columns} dataSource={dataSource} bordered pagination={false} />
                            </div>
                        ),
                        onOk() {
                            if (!checkCode('office:recruitment:setting:field')) {
                                message.warning('暂无权限！');
                                return;
                            }
                            history.push('/office/personnelManagement/recruitment/setting?activeKey=3');
                            resolve(true);
                        },
                        onCancel() {
                            resolve(true);
                        },
                        okText: '立即配置',
                        cancelText: '暂不处理',
                        width: 800
                    });
                }

                if (String(checkType) === '2') {
                    Modal.confirm({
                        title: '提示',
                        content: '对外岗位信息不全，请补充完整！',
                        onOk() {
                            if (!checkCode('office:recruitment:channel:outward:edit')) {
                                message.warning('暂无权限！');
                                return;
                            }
                            history.push(
                                `/office/personnelManagement/recruitment/channel/post/form?recruitCode=${itemData.recruitCode}`
                            );
                            resolve(true);
                        },
                        onCancel() {
                            resolve(true);
                        },
                        okText: '立即补充',
                        cancelText: '取消'
                    });
                }
            } else {
                resolve(true);
                message.error(msg);
            }
        });
    };

    return { checkError };
}
